package com.hemaapp.yjnh.adapter;

import java.math.BigDecimal;
import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

import com.android.uu.utils.uuUtils;
import com.hemaapp.yjnh.BaseAdapter;
import com.hemaapp.yjnh.BaseApplication;
import com.hemaapp.yjnh.BaseUtil;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.activity.FarmerDetailsActivity;
import com.hemaapp.yjnh.activity.MyFarmerActivity;
import com.hemaapp.yjnh.listener.OnItemClickListener;
import com.hemaapp.yjnh.bean.Footprint;
import com.hemaapp.yjnh.bean.User;
import com.hemaapp.yjnh.utils.ImageUtils;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

/**足迹列表适配器
 * @author GuiyouZhang
 * Date：2015年12月15日 下午2:03:01
 * Email:guiyouzhang@163.com
 */
public class FootprintAdapter extends BaseAdapter {

	private Context mContext;
	private ArrayList<Footprint> mDatas = new ArrayList<>();//足迹中的数据
	private int type = 1;//1实物商品，2预约类商品，3田家
    private OnItemClickListener itemClickListener;

	public FootprintAdapter(Context context, ArrayList<Footprint> footprints, int type) {
		super(context);
		this.mContext = context;
		this.mDatas = footprints;
        this.type = type;
	}

    public void setClickListener(OnItemClickListener clickListener) {
        this.itemClickListener = clickListener;
    }

    @Override
	public int getCount() {
		return mDatas.size() == 0 ? 1 : mDatas.size();
	}

	@Override
	public boolean isEmpty() {
        int size = mDatas == null? 0:mDatas.size();
        return size == 0;
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (isEmpty()) 
			return getEmptyView(parent);
		if (type == 1) {
            ViewHolder1 holder;
            if (convertView == null || convertView.getTag(R.id.TAG_VIEWHOLDER) == null) {
                convertView = LayoutInflater.from(mContext).inflate(
                        R.layout.item_cmn_production, parent, false);
                holder = new ViewHolder1(convertView);
                convertView.setTag(R.id.TAG_VIEWHOLDER, holder);
            } else
                holder = (ViewHolder1) convertView.getTag(R.id.TAG_VIEWHOLDER);
            holder.setDatas(position);
		}else if (type == 2) {
            ViewHolder2 holder;
            if (convertView == null || convertView.getTag(R.id.TAG_VIEWHOLDER) == null) {
                convertView = LayoutInflater.from(mContext).inflate(
                        R.layout.item_cmn_book_production, parent, false);
                holder = new ViewHolder2(convertView);
                convertView.setTag(R.id.TAG_VIEWHOLDER, holder);
            } else
                holder = (ViewHolder2) convertView.getTag(R.id.TAG_VIEWHOLDER);
            holder.setDatas(position);
        }else {
            ViewHolder3 holder;
            if (convertView == null || convertView.getTag(R.id.TAG_VIEWHOLDER) == null) {
                convertView = LayoutInflater.from(mContext).inflate(
                        R.layout.item_farmer_list, parent, false);
                holder = new ViewHolder3(convertView);
                convertView.setTag(R.id.TAG_VIEWHOLDER, holder);
            } else
                holder = (ViewHolder3) convertView.getTag(R.id.TAG_VIEWHOLDER);
            holder.setDatas(position);
        }
		return convertView;
	}

    /**
     * 实物类商品
     */
    class ViewHolder1 {
        @Bind(R.id.iv_bkg)
        ImageView ivBkg;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.tv_sales)
        TextView tvSales;
        @Bind(R.id.tv_price)
        TextView tvPrice;
        @Bind(R.id.tv_price_old)
        TextView tvPriceOld;
        @Bind(R.id.tv_size)
        TextView tvSize;
        @Bind(R.id.tv_cash)
        TextView tvCash;
        @Bind(R.id.tv_food_stamp)
        TextView tvFoodStamp;
        @Bind(R.id.iv_shopping_cart)
        ImageView ivShoppingCart;
        int position = -1;

        ViewHolder1(View view) {
            ButterKnife.bind(this, view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (position != -1 && itemClickListener != null)
                        itemClickListener.onClickListener(position);
                }
            });
//            ivShoppingCart.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    if (position != -1 && itemClickListener != null)
//                        itemClickListener.onCartListener(position);
//                }
//            });
        }

        public void setDatas(int position) {
            this.position = position;
            Footprint blog = mDatas.get(position);
            ImageUtils.loadRadiusImage(mContext, blog.getImgurl(), ivBkg, 10);
            tvName.setText(blog.getBlog_name());
            tvSales.setText("已售："+blog.getSellcount());
            tvPrice.setText("¥" + uuUtils.formatAfterDot2(blog.getPrice()));
            tvPriceOld.setText("¥" + uuUtils.formatAfterDot2(blog.getOldprice()));
            tvPriceOld.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG ); //中间横线
//            tvSize.setText("规格:1"+blog.getUnit());
            tvSize.setVisibility(View.GONE);
            tvCash.setText("¥"+blog.getPrice()+"+");
            tvFoodStamp.setText(blog.getScore()+"粮票");
            if (blog.getType().equals("1")) {//田家农货时，不显示购物车
                ivShoppingCart.setVisibility(View.GONE);
            }else
                ivShoppingCart.setVisibility(View.GONE);

        }
    }

    class ViewHolder2 {
        @Bind(R.id.iv_bkg)
        ImageView ivBkg;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.tv_sales)
        TextView tvSales;
        @Bind(R.id.tv_price)
        TextView tvPrice;
        @Bind(R.id.tv_price_old)
        TextView tvPriceOld;
        @Bind(R.id.tv_distance)
        TextView tvDistance;
        @Bind(R.id.tv_cash)
        TextView tvCash;
        @Bind(R.id.tv_food_stamp)
        TextView tvFoodStamp;
        int position = -1;

        ViewHolder2(View view) {
            ButterKnife.bind(this, view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (itemClickListener != null && position != -1) {
                        itemClickListener.onClickListener(position);
                    }
                }
            });
        }

        private void setDatas(int position) {
            this.position = position;
            Footprint blog = mDatas.get(position);
            ImageUtils.loadRadiusImage(mContext, blog.getImgurl(), ivBkg, 10);
            tvName.setText(blog.getBlog_name());
            tvPrice.setText("¥" + uuUtils.formatAfterDot2(blog.getPrice()));
            tvPriceOld.setText("¥" + uuUtils.formatAfterDot2(blog.getOldprice()));
            tvPriceOld.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG ); //中间横线
            tvSales.setText("已售："+blog.getSellcount());
//            tvDistance.setText(blog.getDistance()+"km");
//            tvFoodStamp.setText(uuUtils.formatAfterDot2(blog.getScore()));
            if (TextUtils.isEmpty(BaseUtil.transNull(blog.getDistance())))
                tvDistance.setVisibility(View.GONE);
            else {
                tvDistance.setVisibility(View.VISIBLE);
                tvDistance.setText(blog.getDistance()+"km");
            }
            double price = Double.parseDouble(blog.getPrice());
            double score = Double.parseDouble(blog.getScore());
            BigDecimal bg = new BigDecimal(price-score);
            double cash = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            tvCash.setText("¥"+cash+"+");
            tvFoodStamp.setText(blog.getScore()+"粮票");
        }
    }

    class ViewHolder3 {
        @Bind(R.id.iv_bkg)
        ImageView ivBkg;
        @Bind(R.id.iv_avatar)
        ImageView ivAvatar;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.ratingBar)
        RatingBar ratingBar;
        @Bind(R.id.tv_content)
        TextView tvContent;

        int position = -1;

        ViewHolder3(View view) {
            ButterKnife.bind(this, view);
            view.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (position == -1)
                        return;
                    Intent intent = new Intent();
                    intent.putExtra("client_id", mDatas.get(position).getFarmer_id());
                    intent.setClass(mContext, FarmerDetailsActivity.class);
                    User user = BaseApplication.getInstance().getUser();
                    if (user != null) {
                        String id = user.getId();
                        if (id.equals(mDatas.get(position).getFarmer_id())) {
                            intent.setClass(mContext, MyFarmerActivity.class);
                        }
                    }
                    mContext.startActivity(intent);
                }
            });
        }

        private void setDatas(int position) {
            this.position = position;
            Footprint client = mDatas.get(position);
            ImageUtils.loadAvatar(mContext, client.getAvatar(),ivAvatar);
            if (client.getFarmerImgItemses() != null && client.getFarmerImgItemses().size()> 0)
                ImageUtils.loadRadiusImageMid(mContext, client.getFarmerImgItemses().get(0).getImgurl(), ivBkg, 10);
            String star = client.getStarscore();
            if (!TextUtils.isEmpty(star)) {
                ratingBar.setRating(Float.parseFloat(star));
            }
            tvName.setText(client.getNickname());
            if (isNull(client.getGood_names()))
                tvContent.setText("田家没有提供商品定制");
            else
                tvContent.setText("田家推出"+client.getGood_names()+"等商品定制");
        }
    }
}
